home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7024 < prev    next >
Encoding:
Text File  |  1996-08-05  |  9.4 KB  |  360 lines

  1. Path: newsfeed.direct.ca!usenet
  2. From: etoivane@direct.ca (Ed Toivanen)
  3. Newsgroups: comp.lang.c
  4. Subject: Warning 300 lines of code, and I don't know where the problem is!
  5. Date: 17 Feb 1996 16:44:41 GMT
  6. Organization: Your Organization
  7. Message-ID: <4g50lp$ejf@aphex.direct.ca>
  8. NNTP-Posting-Host: 204.174.243.117
  9. Mime-Version: 1.0
  10. Content-Type: Text/Plain; charset=US-ASCII
  11. X-Newsreader: WinVN 0.99.6
  12.  
  13. I don't know where the problem lies in my code.  It prints out garbage to the 
  14. screen the first time option 5 is selected, but works fine from that point 
  15. on.  Also, when I change the code by adding a printf() in a "safe" spot, such 
  16. as the debug lines, it outputs garbage again!  It is a small, flat database of 
  17. students and their grades.  Only options 0, 1, 4, 5 have been implemented.0= 
  18. quit, 1= add a record, 4= print a record, 5= print the entire database.      
  19. Can you help?
  20.  
  21. Thanks, Ed
  22.  
  23. assign2.h--------------------------------------
  24. /*
  25.         Assign2.h
  26.         
  27.         Ed Toivanen
  28.         Assign2
  29.         Comp 3425(Wed)
  30.         Feb 5, 1996
  31. */
  32. #define NAME_LEN 40
  33. #define MAX_STUDENTS 100
  34.  
  35. typedef enum tagBool {FALSE, TRUE} bool;
  36.  
  37. typedef enum tagAction {QUIT, ADD_REC, DELETE_REC, EDIT_REC, PRINT_REC, 
  38. PRINT_DB, FIND_REC} ACTION;
  39.  
  40. typedef enum tagProgram{SCIENCE, ART} PROGRAM;
  41.  
  42. typedef struct tagScience_mark {
  43.     int math, physics, compsci; /* Range: 0..100 */
  44. }SCIENCE_MARK;
  45.  
  46. typedef struct tagArt_mark {
  47.     int acting, dancing;        /* Range: 0..100 */
  48. }ART_MARK;
  49.  
  50. typedef union tagMark {
  51.     SCIENCE_MARK scientist;
  52.     ART_MARK artist;
  53. }MARK;
  54.  
  55. typedef struct tagStudent_record {
  56.     int id;                     /* Range: 1..99, Unique */
  57.     char name[NAME_LEN + 1];    /* Non-unique */
  58.     PROGRAM major;
  59.     MARK marks;
  60. } STUDENT_RECORD;
  61.  
  62. typedef struct tagIndex {
  63.     long fpos;
  64.     int id;
  65.     char name[NAME_LEN + 1];
  66. } INDEX;    
  67.  
  68. ACTION queryUser(void);
  69. bool doAction(FILE*, STUDENT_RECORD*, ACTION, INDEX[]);    
  70. bool initList(char*, FILE*);
  71. bool getInfo(STUDENT_RECORD*);
  72. bool addRec(FILE*, STUDENT_RECORD*);
  73. int writeRec(FILE*, STUDENT_RECORD*);
  74. bool deleteRec(FILE*, STUDENT_RECORD*);
  75. bool findRec(FILE*, STUDENT_RECORD*);
  76. bool printList(FILE*);
  77. bool printRec(FILE*, STUDENT_RECORD*, INDEX*, int);
  78. bool updateIndex(FILE*, STUDENT_RECORD*, INDEX[]);
  79. bool printDb(FILE*, STUDENT_RECORD*, INDEX*);
  80. int getFileSize(FILE*);  
  81.         
  82.  
  83.  
  84. assign2.c----------------------------------------------
  85. /*
  86.         assign2.c
  87.         
  88.         Ed Toivanen
  89.         Assign 2
  90.         Comp 3425(Fri)
  91.         Feb 5, 1996
  92. */
  93.  
  94. #include <stdio.h>
  95. #include <stdlib.h>
  96. #include <string.h>
  97. #include "assign2.h"
  98.  
  99. int main(void){
  100.     char indexList[13 + 1];
  101.     FILE file, * fp;
  102.     STUDENT_RECORD rec,* pRec;
  103.     INDEX index[MAX_STUDENTS];
  104.     ACTION action;
  105.  
  106.     fp = &file;
  107.     pRec = &rec;    
  108.  
  109.     printf("Enter database file to open\n");
  110.     gets(indexList);
  111.     strcat(indexList, ".dat");
  112.     /*fflush(stdout);*/
  113.  
  114.     fp = fopen(/*indexList*/"student.dat", "a+b");
  115.     if(fp == NULL){
  116.         fprintf(stderr, "%s NULL fp\n", indexList);
  117.         return(1);
  118.     }    
  119.      
  120.     while(action = queryUser()){
  121.         /*fflush(stdin);*/
  122.         doAction(fp, pRec, action, index);
  123.         updateIndex(fp, pRec, index); 
  124.     }
  125.     fclose(fp);
  126.     printf("End of program\n");   
  127.     return(0);
  128. }/*end main()*/ 
  129.  
  130. bool initList(char* list, FILE* fp){
  131.     if(!(fp = fopen(list, "wb"))){
  132.         fprintf(stderr, "Error opening %s\n", list);
  133.         fclose(fp);
  134.         return(FALSE);
  135.     }
  136.     else
  137.         return(TRUE);
  138. }/*end initList()*/       
  139.  
  140. bool getInfo(STUDENT_RECORD* rec){
  141.     char buf[100];
  142.     int num;
  143.     
  144.     printf("Student id:\t");
  145.     fgets(buf, sizeof(buf), stdin);
  146.     printf("\n");
  147.     rec->id=atoi(buf);  
  148.     
  149.     printf("Student's last name first:\t");
  150.     fgets(buf, sizeof(buf), stdin);
  151.     /*buf[strlen(buf)] = '\0';
  152.     printf("\n");*/
  153.     strcpy(rec->name, buf);    
  154.  
  155.     printf("Science(press 1) or Art(press 2)?:\t");
  156.     /*while(!((num=atoi(fgets(buf, sizeof(buf), stdin)) > 0) && (num < 3)))
  157.          {}*/
  158.     num = atoi(fgets(buf, sizeof(buf), stdin));     
  159.     if(num == 1)
  160.         rec->major = SCIENCE;
  161.     else
  162.         rec->major = ART;
  163.     
  164.     printf("Grades\n");
  165.     switch(rec->major){
  166.         case SCIENCE:
  167.                printf("\tMath:\t");
  168.                fgets(buf, sizeof(buf), stdin);
  169.                printf("\n");
  170.                rec->marks.scientist.math = atoi(buf);
  171.                 
  172.                printf("\tPhysics:\t");
  173.                fgets(buf, sizeof(buf), stdin);
  174.                printf("\n");
  175.                rec->marks.scientist.physics = atoi(buf); 
  176.             
  177.                printf("\tCompsci:\t");
  178.                fgets(buf, sizeof(buf), stdin);
  179.                printf("\n");
  180.                rec->marks.scientist.compsci = atoi(buf);
  181.                break;
  182.                
  183.         case ART:
  184.                printf("\tActing:\t");
  185.                fgets(buf, sizeof(buf), stdin);
  186.                printf("\n");
  187.                rec->marks.artist.acting = atoi(buf);
  188.                
  189.                printf("\tDancing:\t");
  190.                fgets(buf, sizeof(buf), stdin);
  191.                printf("\n");
  192.                rec->marks.artist.dancing = atoi(buf);
  193.                break;
  194.                
  195.         default:
  196.                break;
  197.     }/*end switch*/        
  198.     return(TRUE);
  199. }/*End getInfo()*/
  200.  
  201. int writeRec(FILE* fp, STUDENT_RECORD* rec){
  202.     int i;
  203.  
  204.     fseek(fp, 0, SEEK_END);
  205.     if(i=fwrite(rec, sizeof(*rec), 1, fp)!=NULL)
  206.         ;
  207.     else
  208.         fprintf(stderr,"Error writing to file. %d records written\n", i);
  209.     return(i);
  210. }/*end writeRec()*/
  211.  
  212. ACTION queryUser(void){
  213.     char buf[3];
  214.     
  215.     printf("Please enter selection number\n");
  216.     printf("\tQuit\t\t0\n\n");
  217.     printf("\tAdd a record\t 1 \n\n");
  218.     printf("\tDelete a record\t 2 \n\n");
  219.     printf("\tEdit a record\t 3 \n\n");
  220.     printf("\tPrint a record\t 4 \n\n");
  221.     printf("\tPrint out the entire database\t 5 \n\n");
  222.     printf("\t?:");
  223.  
  224.     fgets(buf, sizeof(buf), stdin);
  225.     printf("\n");
  226.     return((ACTION)atoi(buf));
  227. }/*end queryUser()*/
  228.  
  229. bool doAction(FILE* fp, STUDENT_RECORD* rec, ACTION action, INDEX index[]){
  230.     char sBuf[10];
  231.     int iRecNum;
  232.     bool bResult;
  233.     switch(action){
  234.         case QUIT:
  235.             bResult = FALSE;
  236.             break;
  237.  
  238.         case ADD_REC:
  239.             addRec(fp, rec);
  240.             bResult = TRUE;                                     
  241.             break;
  242.  
  243.         case DELETE_REC:
  244.             bResult = deleteRec(fp, rec) ? TRUE : FALSE;
  245.             break;
  246.  
  247.         case EDIT_REC:
  248.             addRec(fp, rec);
  249.             bResult = TRUE;            
  250.             break;
  251.  
  252.         case PRINT_REC:
  253.             printf("\tRecord number?:\t");
  254.             iRecNum = atoi(fgets(sBuf, sizeof(sBuf), stdin));
  255.             /*bResult =*/ printRec(fp, rec, index, iRecNum) /*? TRUE : FALSE*/;
  256.             bResult=TRUE;
  257.             break;
  258.  
  259.         case PRINT_DB:
  260.             /*bResult =*/ printDb(fp, rec, index) /*? TRUE : FALSE*/;
  261.             bResult=TRUE;
  262.             break;
  263.  
  264.         default:
  265.             bResult = FALSE;
  266.             break;
  267.     }/*end switch*/
  268.     printf("\n");
  269.     return(bResult);
  270. }/*end doAction()*/
  271.  
  272. int getFileSize(FILE* fp){
  273.     int iSavedPos, iFileSize;
  274.     
  275.     iSavedPos = ftell(fp);
  276.     fseek(fp, 0L, SEEK_END);
  277.     iFileSize = ftell(fp);
  278.     fseek(fp, iSavedPos, SEEK_SET);
  279.     return(iFileSize);
  280. }    
  281.  
  282. bool updateIndex(FILE* fp, STUDENT_RECORD* rec, INDEX index[]){
  283.     int i=0, fileSize, recSize;
  284.     FILE *file;
  285.     
  286.     fileSize = getFileSize(fp);
  287.     recSize = sizeof(*rec);
  288.  
  289.     while(!feof(fp)){
  290.          fseek(fp, i * recSize, SEEK_SET);
  291.          index[i].fpos = ftell(fp);            
  292.          fread(rec, 1,  recSize, fp);
  293.          index[i].id   = rec->id;
  294.          strcpy(index[i].name, rec->name);
  295.          /*debug
  296.          printf("ftell(fp)=%d\t   rec->id=%d \t rec->name=%s\n", ftell(fp), 
  297. rec->id, rec->name); 
  298.          printf("fpos=%d\t   id=%d\t name=%s\n", index[i].fpos, index[i].id, 
  299. index[i].name);
  300.          end debug*/
  301.          i++;
  302.     }         
  303.  
  304.     return(TRUE);
  305. }/*end updateIndex()*/
  306.  
  307. bool printRec(FILE* fp, STUDENT_RECORD* rec, INDEX index[], int i){
  308.     fseek(fp, index[i].fpos, SEEK_SET);
  309.     fread(rec, 1, sizeof(*rec), fp);
  310.     printf("%d\n", rec->id);
  311.     printf("%s", rec->name);
  312.     switch(rec->major){
  313.         case SCIENCE:
  314.              printf("Science major\n");
  315.              printf("%d\n", rec->marks.scientist.math);
  316.              printf("%d\n", rec->marks.scientist.physics);
  317.              printf("%d\n", rec->marks.scientist.compsci);
  318.              break;
  319.              
  320.         case ART:
  321.              printf("Art major\n");
  322.              printf("%d\n", rec->marks.artist.acting);
  323.              printf("%d\n", rec->marks.artist.dancing);
  324.              break;
  325.              
  326.         default:
  327.              break;
  328.     }/*end switch*/
  329.     printf("\n");                               
  330.     return(TRUE);
  331. }/*end printRec()*/
  332.  
  333. bool printDb(FILE* fp, STUDENT_RECORD* rec, INDEX* index){
  334.     int i, iFileSize, iRecSize;
  335.  
  336.     iFileSize = getFileSize(fp);
  337.     iRecSize = sizeof(*rec);
  338.     fseek(fp, 0L, SEEK_SET);
  339.     for(i=0; i * iRecSize < iFileSize; i++){
  340.         printRec(fp, rec, index, i);
  341.     }        
  342.     return(TRUE);
  343. }/*end printDb()*/
  344.  
  345. bool findRec(FILE* fp, STUDENT_RECORD* rec){
  346.     return(TRUE);
  347. }/*end rindRec()*/    
  348.                        
  349. bool addRec(FILE * fp, STUDENT_RECORD * rec){
  350.     getInfo(rec);
  351.     writeRec(fp, rec);
  352.     return(TRUE);
  353. }       
  354.  
  355. bool deleteRec(FILE* fp, STUDENT_RECORD* rec ){
  356.     return(TRUE);
  357. }/*end deleteRec()*/                                  
  358.  
  359.  
  360.